package com.example.demo.service.impl;

import com.example.demo.convert.ProductConvert;
import com.example.demo.domain.app.vo.CategoryInfoVO;
import com.example.demo.domain.entity.ProductCategoryDO;
import com.example.demo.domain.param.ProductCategoryCreateParam;
import com.example.demo.domain.param.ProductCategoryUpdateParam;
import com.example.demo.domain.vo.CategorySelectVO;
import com.example.demo.domain.vo.CategoryVO;
import com.example.demo.dao.ProductCategoryMapper;
import com.example.demo.service.ProductCategoryService;
import com.example.demo.util.TreeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * CategoryServiceImpl
 *
 * @author haochen78
 * @date 2025/10/7 21:09
 **/
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<CategoryVO> list() {
        List<ProductCategoryDO> categoryList = productCategoryMapper.selectList(null);
        if (CollectionUtils.isEmpty(categoryList)) {
            return Collections.emptyList();
        }
        List<CategoryVO> voList = ProductConvert.INSTANCE.categoryDoToVoList(categoryList);
        return TreeUtils.buildTree(voList, null);
    }

    @Override
    public List<CategorySelectVO> type() {
        List<ProductCategoryDO> categoryList = productCategoryMapper.selectList(null);
        if (CollectionUtils.isEmpty(categoryList)) {
            return Collections.emptyList();
        }
        List<CategorySelectVO> voList = ProductConvert.INSTANCE.categoryDoToSelectVoList(categoryList);
        return TreeUtils.buildTree(voList, null);
    }

    @Override
    public void create(ProductCategoryCreateParam param) {
        productCategoryMapper.insert(ProductConvert.INSTANCE.categoryCreateParToDo(param));
    }

    @Override
    public void update(ProductCategoryUpdateParam param) {
        productCategoryMapper.updateById(ProductConvert.INSTANCE.categoryUpdateParToDo(param));
    }

    @Override
    public void delete(Long id) {
        productCategoryMapper.deleteById(id);
    }

    @Override
    public List<CategoryInfoVO> all() {
        List<ProductCategoryDO> productCategoryList = productCategoryMapper.selectList(null);
        return ProductConvert.INSTANCE.categoryDo2VoList(productCategoryList);
    }
}
